<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>企业资源管理系统 - 登录</title>
    <link rel="shortcut icon" href="../../favicon.ico">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@mdi/font@6.5.95/css/materialdesignicons.min.css"/>

    <style>
        :root {
            --primary-color: #4CAF50;
            --hover-color: #81C784;
            --text-primary: #2E7D32;
            --background-color: #F1F8E9;
        }
        body {
            margin: 0;
            padding: 0;
            font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .login-container {
            width: 400px;
            padding: 40px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
        }
        .login-title {
            text-align: center;
            margin-bottom: 30px;
            color: var(--primary-color);
            font-size: 24px;
            font-weight: bold;
        }
        .login-btn {
            width: 100%;
            margin-top: 20px;
        }
        .footer {
            text-align: center;
            margin-top: 20px;
            font-size: 12px;
            color: #909399;
        }
    </style>
</head>

<body>
<div class="login-box"id="login-app">
    <div class="login-title">
        <i class="mdi mdi-school" style="font-size: 32px; vertical-align: middle;"></i>
        <span style="vertical-align: middle;">企业资源管理系统</span>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
        <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"
                      maxlength="20" prefix-icon="iconfont icon-user"/>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码"
                      prefix-icon="iconfont icon-lock" maxlength="20" @keyup.enter.native="handleLogin"/>
        </el-form-item>
        <el-form-item style="width:100%;">
            <el-button :loading="loading" class="login-btn" size="medium" type="primary"
                       style="width:100%;" @click.native.prevent="handleLogin">
                <span v-if="!loading">登录</span>
                <span v-else>登录中...</span>
            </el-button>
        </el-form-item>
    </el-form>

    <div class="footer">
        © 2025 企业资源管理系统 版权所有
    </div>
</div>


<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../api/login.js"></script>

<script>
    new Vue({
        el: '#login-app',
        data() {
            return {
                loginForm: {
                    username: 'zhangsan',
                    password: '123456'
                },
                loading: false
            }
        },
        computed: {
            loginRules() {
                const validateUsername = (rule, value, callback) => {
                    if (value.length < 1) {
                        callback(new Error('请输入用户名'))
                    } else {
                        callback()
                    }
                };
                const validatePassword = (rule, value, callback) => {
                    if (value.length < 6) {
                        callback(new Error('密码必须在6位以上'))
                    } else {
                        callback()
                    }
                };
                return {
                    'username': [{
                        'validator': validateUsername,
                        'trigger': 'blur'
                    }],
                    'password': [{
                        'validator': validatePassword,
                        'trigger': 'blur'
                    }]
                }
            }
        },
        methods: {
            async handleLogin() {
                this.$refs.loginForm.validate(async (valid) => {
                    if (valid) { //true 前台正则表达式满足
                        this.loading = true;
                        let res = await loginApi(this.loginForm);//等待ajax获得响应后在往下指定，res  =  response.data
                        if (String(res.code) === '1') {
                            // 栈内存，存在浏览器上数据，类似于session
                            window.localStorage.setItem('userInfo', JSON.stringify(res.data));
                            window.location.href = '/back/index.html';
                        } else {
                            this.$message.error(res.msg);
                            this.loading = false;
                        }
                    }
                })
            }
        }
    })
</script>
</body>

</html>